#include "Precompiled.h"
#include "../include/XCrashReporter.h"
#include "client/windows/handler/exception_handler.h"
static google_breakpad::ExceptionHandler* handler = NULL;
static wchar_t UrlStr[256];
static bool gHaveSender = false;
static std::map<std::wstring,std::wstring> gParmMap;
XCrashReporter::Callback XCrashReporter::CallbackFunc = NULL;

//------------------------------------------------------------------------------
bool ShowDumpResults(const wchar_t* dump_path,
					 const wchar_t* minidump_id,
					 void* context,
					 EXCEPTION_POINTERS* exinfo,
					 MDRawAssertionInfo* assertion,
					 bool succeeded) 
{
	if (XCrashReporter::CallbackFunc)
	{
		XCrashReporter::CallbackFunc();
	}
	std::wstring fileName(minidump_id);
	fileName+= L".dmp";

	if(succeeded && gHaveSender)
	{
		google_breakpad::CrashReportSender sender(L"");
		google_breakpad::ReportResult res = sender.SendCrashReport(UrlStr, gParmMap,fileName,NULL);
	}
	return succeeded;
}

//------------------------------------------------------------------------------
bool XCrashReporter::InitializeInProcess( const wchar_t* _pathStr, Callback _callBack/* = NULL*/)
{
	if (handler)
		return true;
	CallbackFunc = _callBack;
	handler = new google_breakpad::ExceptionHandler(_pathStr,
                                 NULL,
								 ShowDumpResults,
                                 NULL,
								 google_breakpad::ExceptionHandler::HANDLER_ALL);
	 if (handler)
		return true;
	 return false;
}
//------------------------------------------------------------------------------
void XCrashReporter::SetSender( const wchar_t* _UrlStr )
{
	if (!_UrlStr)
		return;

	wcsncpy(UrlStr, _UrlStr, 256);
	gHaveSender = true;
}
//------------------------------------------------------------------------------
void XCrashReporter::AddSenderParm( const wchar_t* _key, const wchar_t* _value )
{
	gParmMap.insert(std::pair<std::wstring, std::wstring>(_key, _value));
}

//------------------------------------------------------------------------------
